package com.commonlib.downloadmgr.ui;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.commonlib.downloadmgr.R;
import com.commonlib.downloadmgr.app.Application;
import com.commonlib.downloadmgr.base.DownloadBaseJob;
import com.commonlib.downloadmgr.entity.Entity;
import com.commonlib.downloadmgr.subclass.DownloadHelper;
import com.commonlib.downloadmgr.subclass.DownloadJob;
import com.commonlib.downloadmgr.subclass.DownloadManager;

public class MainActivity extends Activity implements OnClickListener{
	private static final String TAG = MainActivity.class.getSimpleName();
	private static int times = 0;
	private static final String[] pkgs = new String[]{
		"com.taobao.taobao",
		"com.spriteapp.budejie",
		"com.shuqi.controller",
		"com.netease.cloudmusic",
		"qsbk.app",
		"com.dianping.v1",
		"cn.kuwo.player",
		"com.changba",
		"com.sds.android.ttpod",
		"com.pixcoo.ctface",
		"com.douban.event"
	};
	private static final String[] urls = new String[]{
		"http://fancy.189.cn/res/apprepo/1465/com.taobao.taobao/71/com.taobao.taobao.apk",
		"http://fancy.189.cn/res/apprepo/1917/com.spriteapp.budejie/15/com.spriteapp.budejie.apk",
		"http://fancy.189.cn/res/apprepo/983/com.shuqi.controller/69/com.shuqi.controller.apk",
		"http://fancy.189.cn/res/apprepo/1232/com.netease.cloudmusic/9/com.netease.cloudmusic.apk",
		"http://fancy.189.cn/res/apprepo/2182/qsbk.app/35/qsbk.app.apk",
		"http://fancy.189.cn/res/apprepo/248/com.dianping.v1/570/com.dianping.v1.apk",
		"http://fancy.189.cn/res/apprepo/2426/cn.kuwo.player/5350/cn.kuwo.player.apk",
		"http://fancy.189.cn/res/apprepo/2823/com.changba/400/com.changba.apk",
		"http://fancy.189.cn/res/apprepo/1661/com.sds.android.ttpod/5500/com.sds.android.ttpod.apk",
		"http://fancy.189.cn/res/apprepo/995/com.pixcoo.ctface/20130308/com.pixcoo.ctface.apk",
		"http://fancy.189.cn/res/apprepo/946/com.douban.event/10/com.douban.event.apk"
	};
	private static final String[] titles = new String[]{
		"淘宝官方版",
		"百思不得姐",
		"书旗免费小说",
		"网易云音乐",
		"糗事百科",
		"大众点评_美食酒店团购",
		"酷我音乐播放器",
		"唱吧",
		"天天动听音乐发烧版（播放器）",
		"哎姆明星脸",
		"豆瓣活动"
	};
	
	private Button bnAdd;
	private Random random;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		bnAdd = (Button)findViewById(R.id.bn_add);
		bnAdd.setOnClickListener(this);
		random = new Random();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bn_add:
			randomAddJob();
			break;

		default:
			break;
		}
	}
	
	private void randomAddJob(){
		//TODO
		int index = getDistinctIndex();
		Entity entity = new Entity(titles[index], pkgs[index], urls[index], "1000");
		DownloadJob job = new DownloadJob(Application.self.getApplicationContext(), DownloadHelper.getInstance(), entity, entity.pkgName, entity.url);
		DownloadManager.getInstance(Application.self.getApplicationContext()).addDownloadJob(job);
		Intent intent = new Intent(this, DownloadManagerActivity.class);
		startActivity(intent);
	}
	
	private int getDistinctIndex(){
		Log.v(TAG, "getDistinctIndex, times is " + times);
		int index = random.nextInt(pkgs.length);
		boolean isDuplex = false;
		ArrayList<DownloadBaseJob<Entity>> allJobs = DownloadManager.getInstance(this.getApplicationContext()).getAllJobs();
		if (times++ < 5) {
			for (DownloadBaseJob<Entity> job : allJobs) {
				if (job.getId().equals(pkgs[index])) {
					isDuplex = true;
					break;
				}
			}
		}
		if (isDuplex) {
			index = getDistinctIndex();
		}
		times = 0;
		return index;
	}
	
}